/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import entity.EventInfo;
import entity.Registration;
import entity.UserInfo;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author VietTien
 */
@Stateless
public class RegistrationFacade extends AbstractFacade<Registration> {

    @PersistenceContext(unitName = "BluePumpkin-ejbPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public RegistrationFacade() {
        super(Registration.class);
    }

    public Registration checkTakeIn(UserInfo u, EventInfo e) {
        String sql = "SELECT r FROM Registration r"
                + " WHERE r.eventInfo = :eventInfo AND r.userInfo = :userInfo";

        Query query = em.createQuery(sql);
        query.setParameter("userInfo", u);
        query.setParameter("eventInfo", e);

        List l = query.getResultList();
        if (l.size() > 0) {
            return (Registration) query.getResultList().get(0);
        }
        return null;
    }

    //get Registration of an event
    public List<Registration> findRegByEvent(EventInfo evt) {
        String sql = "SELECT r FROM Registration r"
                + " WHERE r.eventInfo = :eventInfo"
                + " ORDER BY r.regTime";
        Query query = em.createQuery(sql);
        query.setParameter("eventInfo", evt);
        List l = query.getResultList();
        if (l.size() > 0) {
            System.out.println("LIST REG EVT: " + l.size());
            return l;
        }
        return null;
    }

    public List<Registration> findRegByUser(UserInfo user) {
        String sql = "SELECT r FROM Registration r"
                + " WHERE r.userInfo = :userInfo";

        Query query = em.createQuery(sql);
        query.setParameter("userInfo", user);

        List l = query.getResultList();
        if (l.size() > 0) {
            System.out.println("LIST REG USER: " + l.size());
            return l;
        }
        return null;
    }

    public List<Registration> findRegAccepEvent(EventInfo evt) {
        String sql = "SELECT r FROM Registration r"
                + " WHERE r.eventInfo = :eventInfo AND r.statusReg = 'Accept'"
                + " ORDER BY r.regTime";

        Query query = em.createQuery(sql);
        query.setParameter("eventInfo", evt);

        List l = query.getResultList();
        if (l.size() > 0) {
            System.out.println("LIST REG: " + l.size());
            return l;
        }
        return null;
    }

   

    public boolean findEventHasOneParticipate(EventInfo evt) {
        String sql = "SELECT r FROM Registration r"
                + " WHERE r.eventInfo = :eventInfo";

        Query query = em.createQuery(sql);
        query.setParameter("eventInfo", evt);

        List l = query.getResultList();
        if (l.size() > 0) {
            System.out.println("LIST REG: " + l.size());
            return true;
        }
        return false;
    }
    public void createReg(Registration entity, EventInfo evt) {
        em.persist(entity);
        EventInfo event = em.find(EventInfo.class, evt.getEventID());
        event.getRegistrationCollection().add(entity);
    }


}
